<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/controller/interface.php';
require_once $basedir . '/../../../classes/controller/xrd/lrdd.php';

require_once $basedir . '/../../../classes/minime/hrefrenderer.php';
require_once $basedir . '/../../../classes/minime/user.php';
require_once $basedir . '/../../../classes/minime/user/loader.php';

require_once $basedir . '/../../../classes/xrd.php';
require_once $basedir . '/../../../classes/xrd/childelement.php';
require_once $basedir . '/../../../classes/xrd/lrdd.php';
require_once $basedir . '/../../../classes/xrd/lrdd/alias.php';
require_once $basedir . '/../../../classes/xrd/lrdd/subject.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/atom.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/salmon.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/magicpublickey.php';

require_once 'vfsStream/vfsStream.php';

class Controller_LrddTest extends PHPUnit_Framework_TestCase {
    
    public function testConstructorOfLrddController() {
        
        $_GET['uri'] = urlencode("acct:tuomas@lobstermonster.org");
        $_POST['user'] = array();

        $config = "[datastorage]
filepath = vfs://srv/datastorage";
        
        # Register our virtual file system
        vfsStreamWrapper::register();
        
        # Create a new root directory
        $root = new vfsStreamDirectory('srv');
        $root->addChild(new vfsStreamDirectory('datastorage'));
        
        vfsStreamWrapper::setRoot($root);
       
        $conf['datastore']['filepath'] = vfsStream::url('srv/datastorage');
        
        $obj = new Controller_Xrd_Lrdd($_GET, array(), "", $conf);
        $this->assertEquals('Controller_Xrd_Lrdd', get_class($obj));
        
    }
    
    public function testLrddControllerSuccessWorkflow() {
        
        $_GET['uri'] = urlencode("acct:tuomas@lobstermonster.org");
        $_POST = array();
        
        $content = '<?xml version="1.0" encoding="UTF-8"?>
<minimeuser>
  <name>Tuomas Koski</name>
  <jid>koski@boobs.com</jid>
  <uri>http://www.lobstermonster.org/tuomas</uri>
  <encoded-privatekey>RSA.iBamjotepBpArJxeMCM7y8qv3DUQbV_96XscmnBEiIi0LAXr_Ur3HTBnNnLqq7hDU_3xnkfs7f4cq9hz2mXaDO9xk-QLe3lmehTjMLQGqkZCoti3SQ0yVUUjqaJRA8JOXrq2LQqsYdljxyShZ99_EzGQ383xe2aMrH1hq15nFFk=.AQAB.F2Hw70gSFFcwapgZdOLw6MSkL81sS6a1LXNKSFzm4aC_eFryH9zMFddVvJzFJoksBFf7Nmt8Ua4aRgqUaV-bteF2LWWud4uZz5v89pwqmgp6JGDbGfHjwSJvIlucB5IlKq3lIluNWx5v0IPsKpPdCMmaTIP7ckmJ4Ta9lqpAB0M=</encoded-privatekey>
  <encoded-publickey>RSA.iBamjotepBpArJxeMCM7y8qv3DUQbV_96XscmnBEiIi0LAXr_Ur3HTBnNnLqq7hDU_3xnkfs7f4cq9hz2mXaDO9xk-QLe3lmehTjMLQGqkZCoti3SQ0yVUUjqaJRA8JOXrq2LQqsYdljxyShZ99_EzGQ383xe2aMrH1hq15nFFk=.AQAB.</encoded-publickey>
  <title>Hoi, this is a title!</title>
  <subtitle>Hoi, this is a title!</subtitle>
  <logo></logo>
  </minimeuser>
';
        
        # Register our virtual file system
        vfsStreamWrapper::register();
        
        # Create a new root directory
        $root = new vfsStreamDirectory('srv');
        $root->addChild(new vfsStreamDirectory('datastorage'));
        $root->getChild('datastorage')->addChild(new vfsStreamDirectory('user'));
        $root->getChild('datastorage')->getChild('user')->addChild(new vfsStreamDirectory('tuomas'));
        $root->getChild('datastorage')->getChild('user')->getChild('tuomas')->addChild(vfsStream::newFile('user.xml')->withContent($content));
        vfsStreamWrapper::setRoot($root);
       
        $conf['datastore']['user'] = vfsStream::url('srv/datastorage/user');
        $conf['url']['atom'] = "http://www.lobstermonster.org/minime/atom/tuomas.xml";
        $conf['url']['salmon'] = "http://www.lobstermonster.org/minime/salmon/tuomas";
        
        $controller = new Controller_Xrd_Lrdd($_GET, $_POST, "", $conf);
        $controller->execute();
        
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>Tuomas Koski</Subject>
  <Alias>http://www.lobstermonster.org/tuomas</Alias>
  <Link rel="http://webfinger.net/rel/profile-page" href="http://www.lobstermonster.org/tuomas" type="text/html"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" href="http://www.lobstermonster.org/minime/atom/tuomas.xml" type="application/atom+xml"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://www.lobstermonster.org/minime/salmon/tuomas"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.iBamjotepBpArJxeMCM7y8qv3DUQbV_96XscmnBEiIi0LAXr_Ur3HTBnNnLqq7hDU_3xnkfs7f4cq9hz2mXaDO9xk-QLe3lmehTjMLQGqkZCoti3SQ0yVUUjqaJRA8JOXrq2LQqsYdljxyShZ99_EzGQ383xe2aMrH1hq15nFFk=.AQAB."/>
</XRD>
';
        
        $this->assertEquals($expected, $controller->getOutputBody());
        $this->assertArrayHasKey('Content-Type', $controller->getHeaders());
        $this->assertContains('text/xml', $controller->getHeaders());
    }
}